package cn.fxyz.web.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.fxyz.entity.Food;
import cn.fxyz.entity.Foodtype;
import cn.fxyz.page.PageBean;
import cn.fxyz.service.FoodService;
import cn.fxyz.service.FoodtypeService;

@Controller
@RequestMapping("/foodtype")
public class FoodtypeController {
	@Resource
	private FoodtypeService foodtypeService;
	@Resource
	private FoodService foodService;
	
	public FoodService getFoodService() {
		return foodService;
	}
	public void setFoodService(FoodService foodService) {
		this.foodService = foodService;
	}
	@RequestMapping("findTypeAll")
	public String findTypeAll(Model model) {
		List<Foodtype> foodtypes = foodtypeService.findAll();
		model.addAttribute("foodtypes", foodtypes);
		return "reception/detail/caidan";
	}
	private int pageSize = 6;  //固定值
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	@RequestMapping("findAllFood")
	public String findAll(PageBean<Food> pagebean,Food food,Model model,@RequestParam("typeid") Integer typeid) {
		PageBean<Food> pageBean = foodService.findAll(food, pagebean.getPageNum(), pageSize,typeid);
		model.addAttribute("pageBean", pageBean);
		List<Foodtype> foodtypes = foodtypeService.findAll();
		model.addAttribute("foodtypes", foodtypes);
		return "reception/detail/caidan";
	}
	@RequestMapping("findById")
	public String findById(Foodtype tyFoodtype,Model model) {
		Foodtype findFoodtype = foodtypeService.findById(tyFoodtype);
		model.addAttribute("findFoodtype", findFoodtype);
		List<Foodtype> foodtypes = foodtypeService.findAll();
		model.addAttribute("foodtypes", foodtypes);
		return "reception/detail/caidan";
	}
	
	/*@RequestMapping("findAllFood")
	public String findAllFood(Model model) {

		List<Food> foods = foodService.findAll();
		model.addAttribute("foods", foods);
		List<Foodtype> foodtypes = foodtypeService.findAll();
		model.addAttribute("foodtypes", foodtypes);
		return "reception/detail/caidan";
	}*/
	//////////////////////////////////////////
	//后台功能
	//查询所有菜系
	@RequestMapping("findAll")
	public String findAll(Model model){
		List<Foodtype> findAll = foodtypeService.findAll();
		model.addAttribute("foodType", findAll);
		return "backstage/detail/cuisineList";
	}
	
	//添加菜系
	@RequestMapping("add")
	public @ResponseBody String add(Foodtype foodtype){
		Foodtype findType = foodtypeService.findByName(foodtype);
		if(findType != null) {
			return "no";
		}
		foodtypeService.add(foodtype);
		return "success";
		
	}
	
	//删除菜系
	@RequestMapping("remove")
	public String remove(Foodtype foodtype){
		
		foodtypeService.remove(foodtype);
		return "redirect:findAll.do";
	}
	
	//修改菜系
	@RequestMapping("modify")
	public @ResponseBody String update(Foodtype foodtype){
		System.out.println(foodtype.getTypename());
		foodtypeService.modify(foodtype);
		System.out.println(222);
		return "success";
	}
	
	//获取到菜品名称
	@RequestMapping("findone")
	public String findone(Foodtype foodtype,Model model){
		Foodtype findone = foodtypeService.findone(foodtype.getId());
		model.addAttribute("findone", findone);
		return "backstage/detail/updateCuisine";
	}
	public FoodtypeService getFoodtypeService() {
		return foodtypeService;
	}
	public void setFoodtypeService(FoodtypeService foodtypeService) {
		this.foodtypeService = foodtypeService;
	}
	@RequestMapping("checkAdd")
	public @ResponseBody String checkAdd(Foodtype foodtype) {
		Foodtype findType = foodtypeService.findByName(foodtype);
		if(findType != null) {
			return "no";
		}
		return "success";
		
	}
}
